import api from "../../api/api";
import {reactive} from "vue";
import {TdTabPanelProps} from "tdesign-vue-next";
import {MenuDir} from "../../tool/types";

export const Menu = reactive({
    List: new Array<MenuDir>(),
    TabValue: '/admin/my/info',
    Tabs: new Array<TdTabPanelProps>(),
    RouterViewKeepAlive: new Array<string>(),
})


// GetMenu 获取菜单列表
export const GetMenu = async () => {
    let res = await api.User.Menu()
    if (res != null) {
        Menu.List = res.data
    }
}

// AddNewTab 新增标签页
export const AddNewTab = async (name: string) => {
    Menu.TabValue = name
    let index = Menu.Tabs.findIndex(it => {
        return it.value === name
    })
    if (index >= 0) {
        return
    }
    let item: TdTabPanelProps = {}
    for (let i = 0; i < Menu.List.length; i++) {
        for (let j = 0; j < Menu.List[i].items.length; j++) {
            if (name === `/admin/${Menu.List[i].path}/${Menu.List[i].items[j].path}`) {
                item.label = Menu.List[i].items[j].label
                item.value = name
                item.removable = true
                Menu.Tabs.push(item)
                Menu.RouterViewKeepAlive.push(name.substring(7, 1024).replace(/\//g, "_"))
                return
            }
        }
    }
}
